package com.boomsecret.juc.collections;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/23.
 */
public class DelayQueueDemo {
    public static void main(String[] args) {
        DelayQueue<CustomString> delayQueue = new DelayQueue<>();
        delayQueue.add(new CustomString("aa"));
        delayQueue.add(new CustomString("bb"));
        delayQueue.add(new CustomString("vv"));

        for (CustomString customString : delayQueue) {
            System.out.println(customString.getValue());
        }
        System.out.println("------------");

        System.out.println(delayQueue.size());
        System.out.println("------------");

        // delayTime小于等于0 可被获取
        System.out.println(delayQueue.poll());
        System.out.println("------------");

        for (CustomString customString : delayQueue) {
            System.out.println(customString.getValue());
        }
        System.out.println("------------");


    }

    public static class CustomString implements Delayed {
        private String value;

        public CustomString(String value) {
            this.value = value;
        }

        @Override
        public long getDelay(TimeUnit unit) {
            return 0;
        }

        @Override
        public int compareTo(Delayed o) {
            return 0;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "CustomString{" +
                    "value='" + value + '\'' +
                    '}';
        }
    }
}
